const fs = require('fs');
const { exit } = require('process');

function channelInfo(record) {
  const [name, url] = record.split(',');
  return {
    name: name?.trim(),
    url: url?.trim(),
  }
}

function isUrl(url) {
  return /:\/\//.test(url);
}

const sourceContent = fs.readFileSync('./source.txt').toString();
if (!sourceContent) {
  console.error('没有原始数据');
  exit(1);
}
const records = sourceContent.split('\n')
  .map(channelInfo)
  .filter(({ url }) => isUrl(url))
  .map(({ name, url }, idx) => `#EXTINF:-1,${idx} ${name}\n${url}`)
  .join('\n');
const targetContent = `#EXTM3U\n${records}\n`;
fs.writeFileSync('./iptv.m3u8', targetContent);
